package com.samtech.tree;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTPFile;
import org.primefaces.model.TreeNode;

public class FileTreeNode implements TreeNode {
    public static final String DEFAULT_TYPE = "default";
   private FTPFile file;
   private boolean expanded;
   private String type;
     
   private List<TreeNode> children;
   
   private TreeNode parent;
   
   
    public FileTreeNode(FTPFile file){
     this.file=file;   
     children = new ArrayList<TreeNode>();
     type=DEFAULT_TYPE;
    }
    
    public void addChild(TreeNode treeNode) {
        treeNode.setParent(this);
        children.add(treeNode);
    }

    public int getChildCount() {
        return children.size();
       
    }

    public List<TreeNode> getChildren() {
        
        return children;
    }

    public Object getData() {
        String name = file.getName();
        return name;
    }

    public TreeNode getParent() {
        
        return this.parent;
    }

    public String getType() {
       
        return type;
    }

    public boolean isExpanded() {
        
        return this.expanded;
    }

    public boolean isLeaf() {
        if(children==null || children.isEmpty())return true;
        return false;
    }

    public void setExpanded(boolean expanded) {
        this.expanded=expanded;

    }

    public void setParent(TreeNode treeNode) {
       this.parent=treeNode;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((file == null) ? 0 : file.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        FileTreeNode other = (FileTreeNode) obj;
        if (file == null) {
            if (other.file != null)
                return false;
        } else{
            if(file==null && other.file==null)return true;
            if(file!=null && other.file!=null){
                String name = file.getName();
                String name2 = other.file.getName();
                if(!name.equals(name2))
                    return false;
                else{
                    if(this.parent==null && other.parent==null)return true;
                    if(this.parent!=null && other.parent!=null)
                    return this.parent.equals(other.getParent());
                    else return false;
                }
            }else return false;
        }
        return true;
    }
    @Override
    public String toString() {
       if(file!=null){
           return file.getName();
       }
       return "";
    }
}
